package com.ceshiren.hogwarts.wework;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.qameta.allure.Allure;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;


public class BasePage {
    AppiumDriver driver;
    private WebElement currentElement;
    private SimpleDateFormat sdf;


    public BasePage(AppiumDriver driver){
        this.driver=driver;
    }

    public BasePage() {

    }


    public BasePage find(By by){
        try{
            currentElement = driver.findElement(by);
            ExceptionHandler.retry=0;//如果执行成功就将标志清零
        }catch (Exception exception){
            //递归尝试控制
            ExceptionHandler.retry+=1;
            if(ExceptionHandler.retry>ExceptionHandler.max_retry){
                throw exception;
            }
            //todo:处理各种异常
            String pageSource=driver.getPageSource();
            ExceptionHandler.getAllExceptionHandler().forEach((ExceptionHandler exceptionHandler) -> {
                //发现并命中异常
                if (exceptionHandler.isHit(pageSource)) {
                    //处理异常
                    exceptionHandler.handle(driver);
                    //返回正常轨迹
                    find(by);

                }
            });

        }

        return this;
    }
    public BasePage click(By by){
        find(by).click();
        return this;
    }
    public BasePage click(){
        currentElement.click();
        return this;
    }
    public BasePage sendkeys(String input,By by){
        find(by).sendkeys(input);
        return this;
    }

    public BasePage sendkeys(String input) {
        currentElement.sendKeys(input);
        return this;
    }
    public String getText(){
        return currentElement.getText();

    }
    public void wait(ExpectedCondition expectedCondition,int timeout){
        WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(timeout));
        //wait.until(driver->driver.findElements(By.xpath("//*[@text='通讯录']")).size()>0);
        wait.until(expectedCondition);
    }
    public void wait(ExpectedCondition expectedCondition){
        WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(5));
        wait.until(expectedCondition);
    }

    public void takescreenshot(String name) {
        File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(screenShotFile, new File("./" + name+ ".jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public SimpleDateFormat getCurrentDataTime() {
        sdf = new SimpleDateFormat("MMddHHmm");
        return sdf;
    }
    public void attach(String name) throws FileNotFoundException {
        Allure.addAttachment("attachment","截图");
        try{
            Allure.addAttachment("picture","image/jpg", String.valueOf(new FileInputStream("./" + name+ ".jpg")),".jpg");
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }

}

